﻿using Domain.Entities;
using Domain.IRepositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Utility;


namespace EFcore.Repositories
{
    public class ReplyRepository : BaseRepository<Reply>, IReplyRepository
    {
        public ReplyRepository(JFDbContext dbcontext) : base(dbcontext)
        {

        }


        /// <summary>
        /// 根据Id获取实体
        /// </summary>
        /// <param name="id">Id</param>
        /// <returns></returns>
        public Reply GetById(int id)
        {
            var model = _dbContext.Set<Reply>().FirstOrDefault(i => i.Id == id);
            return model;
        }


        /// <summary>
        /// /
        /// </summary>
        /// <param name="id">文章ID</param>
        /// <param name="pid">父级ID</param>
        /// <returns></returns>
        public IQueryable<Reply> GetAll(string id,string pid)
        {
            IQueryable<Reply> list;
            if (string.IsNullOrEmpty(pid))
                pid = "0";
            if (string.IsNullOrEmpty(id))
            {
                return list = _dbContext.Set<Reply>().Where(i => i.Parent_Id == Int32.Parse(pid));
            }
            else
            {
                return list = _dbContext.Set<Reply>().Where(i => i.Parent_Id == Int32.Parse(pid) && i.Article_Id == Int32.Parse(id));
            }
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="userId">用户ID   0或null为所有</param>
        /// <param name="disable">隐藏 0/1</param>
        /// <param name="SearchKeyWords">查询关键字</param>
        /// <param name="PageSize">页面输出信息行数</param>
        /// <param name="PageCurrent">当前页数</param>
        /// <param name="OrderField">排序字段</param>
        /// <param name="OrderDirection">排序</param>
        /// <returns></returns>
        public IQueryable<Reply> GetPageList(string userId, string disable, string SearchKeyWords, int PageSize, int PageCurrent, string OrderField, bool OrderDirection = true)
        {
            //判断查询参数
            Expression<Func<Reply, bool>> where = LinqUtil.True<Reply>();
            if (!string.IsNullOrEmpty(userId) && userId != "0")
            {
                where = where.AndAlso(i => i.User_Id == Int32.Parse(userId));
            }

            //if (!string.IsNullOrEmpty(disable))
            //{
            //    where = where.AndAlso(i => i.Disable == Int32.Parse(disable));
            //}

            //if (!string.IsNullOrEmpty(SearchKeyWords))
            //{
            //    where = where.AndAlso(i => i.Reply_name.Contains(SearchKeyWords));
            //}

            var result = from p in _dbContext.Set<Reply>()
                         select p;
            if (where != null)
                result = result.Where(where);

            if (OrderField != null)
            {
                result = result.OrderBy(OrderField, OrderDirection);
            }
            else
            {
                if (OrderDirection)
                    result = result.OrderBy(m => m.Id);
                else
                    result = result.OrderByDescending(m => m.Id);
            }

            return result.Skip((PageCurrent - 1) * PageSize).Take(PageSize);

        }

        /// <summary>
        /// 根据ID是否存在，新增或修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddOrUpdate(Reply model)
        {
            if (InsertOrUpdate(model, true) != null)
            { return true; }
            else
            { return false; }
        }

        public bool DeleteById(int id)
        {
            try
            {
                Delete(id, true);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
